<?php

namespace App\Http\Controllers;

use App\Exceptions\ValidationException;
use LucaDegasperi\OAuth2Server\Authorizer;

class UserController extends CommonController
{
    public function __construct(Authorizer $authorizer)
    {
        parent::__construct($authorizer);
        $this->middleware('oauth', ['except' => ['store']]);
        $this->middleware('validation');
    }

    private static $_validate = [
        // todo
    ];

    public function store()
    {
        // 用户注册
        return $this->register();
    }

    protected function register()
    {
        $email    = request('email');
        $username = request('username');
        $password = request('password');

        $client = MultiplexController::getWebServiceClient();
        $parameter = [
            'UserName'  => $username,
            'UserPass'  => $password,
            'UserEamil' => $email,
        ];
        $data = $client->RegistUser($parameter);

        $result = json_decode($data->RegistUserResult);
        if (intval($result->RstValue) > 0) {
            // 注册成功
            // $this->saveThirdPartyUser($username, $password);
            return (array) $result;
        }

        throw new ValidationException($result->RstDesc);
    }

}